package com.pignest.hotel.beans;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

import com.pignest.framework.ajax.AjaxObject;
import com.pignest.hotel.annotation.Bean;
import com.pignest.hotel.beans.global.BaseBean;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.Permission;
import com.pignest.hotel.util.DateUtil;


/**
 * 用户
 */
@DataTransferObject
@Bean("员工")
public class User extends BaseBean implements Cloneable, AjaxObject {
	private static final long serialVersionUID = -3748367480628075455L;
	@RemoteProperty
	private String name;		//真实姓名
	@RemoteProperty
	private String workNumber;	//工号
	/** 员工卡的卡序列号 */
	@RemoteProperty
	private String cardNumber;	//卡号
	@RemoteProperty
	private boolean enable;		//是否启用
	@RemoteProperty
	private String gender;		//性别
	private String remark;		//备注
	
	//可自行修改字段
	private String password;	//密码
	private String phone;		//电话
	@RemoteProperty
	private String nickName;	//昵称
	private Date birthday;		//生日
	
	//系统字段
	private Date joinTime;			//加入时间, 即创建时间
	private Date lastLoginTime;		//上次登录时间
	private Date currentLoginTime;	//当前登录时间
	private boolean reserve;
	
//	private String orgid;
//	private String areaid;
//	private String duty;
//	private String mail;
//	private Date birthday;
//	private String nativePlace;
//	private String qq;
//	private String msn;
//	private String workPhone;
//	private String homePhone;
//	private String homeAddress;
//	private String homePost;
	
	public User() {
	}
	
	/**
	 * @param name 昵称
	 * @param workNo 工号
	 * @param password 密码
	 * @param cardNo 卡号
	 */
	public User(String name, String workNo, String password, String cardNo) {
		this.name = name;
		this.cardNumber = cardNo;
		this.workNumber = workNo;
		this.password = password;
		this.enable = true;
	}
	
	public User(String name, String workNo) {
		this.name = name;
		this.workNumber = workNo;
		this.enable = true;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCardNumber() {
		return this.cardNumber;
	}

	public void setCardNumber(String cardNo) {
		this.cardNumber = cardNo;
	}

	public String getWorkNumber() {
		return this.workNumber;
	}

	public void setWorkNumber(String workNo) {
		this.workNumber = workNo;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String mobile) {
		this.phone = mobile;
	}

	public String getGender() {
		return this.gender;
	}

	public void setGender(String sex) {
		this.gender = sex;
	}

	public boolean isEnable() {
		return enable;
	}
	
	public boolean getEnable() {
		return enable;
	}
	
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	
	public String getRemark() {
		return remark;
	}
	
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public Date getJoinTime() {
		return joinTime;
	}

	public void setJoinTime(Date joinTime) {
		this.joinTime = joinTime;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public Date getCurrentLoginTime() {
		return currentLoginTime;
	}

	public void setCurrentLoginTime(Date currentLoginTime) {
		this.currentLoginTime = currentLoginTime;
	}
	
	public boolean isReserve() {
		return reserve;
	}
	
	public void setReserve(boolean reserve) {
		this.reserve = reserve;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////
	//逻辑
	
	@RemoteProperty
	public String getUserId() {
		return getId();
	}
	
	public void setPermissions(Collection<String> permissions) {
		this.permissions = permissions;
	}

	public User clone() {
		User n = null;
		try {
			n = (User)super.clone();
		} catch (CloneNotSupportedException e) {
		}
		return n;
	}
	
	public String getColor() {
		return enable ? "red" : "blue";
	}
	
	private Collection<String> permissions;
	public Collection<String> getPermissions() {
		if (this.id == null)
			return null;
		if (this.permissions == null) {
			this.permissions = new HashSet<String>();
//			User user = Services.getUserService().get(User.class, this.id);
			List<Role> roles = Services.getRoleService().getRolesByUser(this);
			if (roles != null && !roles.isEmpty()) {
				for (Role role : roles) {
					Collection<ACL> acls = role.getAcls();
					if (acls == null || acls.isEmpty())
						continue;
					for (ACL acl : acls) {
						permissions.add(acl.getPermission());
					}
				}
			}
		}
		return this.permissions;
	}
	
	public String getBirthday2() {
		if (birthday == null)
			return "";
		return DateUtil.toString(birthday, DateUtil.DEFAULT_DATE_FORMAT);
	}
	
	public String getLastLoginTime2() {
		if (lastLoginTime == null)
			return "";
		return DateUtil.toString(lastLoginTime, DateUtil.DEFAULT_DATETIME_FORMAT);
	}
	
	public String getDisplayName() {
		if (StringUtils.isNotEmpty(nickName))
			return nickName;
		return name;
	}
	
	public String getFormalName() {
		return new StringBuilder(" [").append(name).append("(工号:").append(workNumber).append(")] ").toString();
	}
	
	public boolean isSuperUser() {
		return reserve && Config.getSuperUserWorkNumber().equals(workNumber);
	}
	
	public boolean hasPermission(String p) {
		return Permission.alow(p, this);
	}
	
	@Override
	public String toSimpleString() {
		return concat(workNumber, name, cardNumber);
	}
}